# -*- coding:utf-8 -*-
# api/business_chain.py

from sanic import Blueprint, request, response
from sanic.log import logger
from sanic_ext import openapi

from business.business_chain import (
    get_business_chain,
    get_business_chain_by_uuid,
    get_business_chain_by_app_sys_id,
    get_association_system_by_app_sys_id
)

bp_business_chain_v1 = Blueprint('BusinessChain', url_prefix='/business_chain', version=1)


@bp_business_chain_v1.get('/')
@openapi.definition(
    tag='应用系统业务链',
    summary='获取应用系统业务链列表',
    description='获取应用系统业务链列表',
    parameter=[],
)
async def get_business_chain_interface(request: request.Request):
    logger.info(f"GET {request.url}")
    data_result = await get_business_chain(request)
    return response.json(dict(data_result), content_type='application/json')


@bp_business_chain_v1.route('/<chain_uuid>', methods=['GET'])
@openapi.definition(
    tag='应用系统业务链',
    summary='根据业务链UUID查询应用系统列表',
    description='根据业务链UUID查询应用系统业务链列表',
    parameter=[
        {
            "name": "chain_uuid",
            "type": "string",
            "schema": "f40fdda284944ada890cc435b29e5827",
            "location": "path",
            "decription": "业务链UUID",
            "required": True,
            "deprecated": None,
            "allowEmptyValue": False,
        }
    ],
)
async def get_business_chain_by_uuid_interface(request: request.Request, chain_uuid: str):
    logger.info(f"GET {request.url}")
    data_result = await get_business_chain_by_uuid(chain_uuid, request)
    return response.json(dict(data_result), content_type='application/json')


@bp_business_chain_v1.route('/filter', methods=['GET'])
@openapi.definition(
    tag='应用系统业务链',
    summary='根据应用系统ID查询相关应用系统所有业务链列表',
    description='根据应用系统ID查询相关应用系统所有业务链列表',
    parameter=[
        {
            "name": "app_sys_id",
            "type": "string",
            "schema": "CS0411_1",
            "location": "query",
            "decription": "应用系统ID",
            "required": True,
            "deprecated": None,
            "allowEmptyValue": False,
        }
    ],
)
async def get_business_chain_by_app_sys_id_interface(request: request.Request):
    logger.info(f"GET {request.url}")
    app_sys_id = request.args.get('app_sys_id')
    data_result = await get_business_chain_by_app_sys_id(app_sys_id, request)
    return response.json(dict(data_result), content_type='application/json')


@bp_business_chain_v1.route('/association_system/filter', methods=['GET'])
@openapi.definition(
    tag='应用系统业务链',
    summary='根据应用系统ID查询所有业务链中上下游关联应用系统信息',
    description='根据应用系统ID查询所有业务链中上下游关联应用系统信息',
    parameter=[
        {
            "name": "app_sys_id",
            "type": "string",
            "schema": "CS0411_1",
            "location": "query",
            "decription": "应用系统ID",
            "required": True,
            "deprecated": None,
            "allowEmptyValue": False,
        }
    ],
)
async def get_association_system_by_app_sys_id_interface(request: request.Request):
    logger.info(f"GET {request.url}")
    app_sys_id = request.args.get('app_sys_id')
    data_result = await get_association_system_by_app_sys_id(app_sys_id, request)
    return response.json(dict(data_result), content_type='application/json')
